#include "rollingcalendar.h"
#include "ui_rollingcalendar.h"

RollingCalendar::RollingCalendar(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RollingCalendar)
{
    ui->setupUi(this);
    //设置窗体透明
    this->setAttribute(Qt::WA_TranslucentBackground, true);
    //设置无边框
    this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    //实例阴影shadow
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    //设置阴影距离
    shadow->setOffset(0, 0);
    //设置阴影颜色
    shadow->setColor(QColor("#444444"));
    //设置阴影圆角
    shadow->setBlurRadius(30);
    //给嵌套QWidget设置阴影
    this->setGraphicsEffect(shadow);
    //给垂直布局器设置边距(此步很重要, 设置宽度为阴影的宽度)
    this->setContentsMargins(24,24,24,24);
    ui->wid_calendar->setStyleSheet("background-color: rgb(255, 255, 255);"
                        "border-top-right-radius:4px;"
                        "border-top-left-radius:4px;");
    ui->wid_button->setStyleSheet("background-color: rgb(255, 255, 255);"
                        "border-bottom-right-radius:4px;"
                        "border-bottom-left-radius:4px;");

    ui->yearwidget->setRange(Enum::YEARUP,Enum::YEARDOWN);
    ui->yearwidget->setSectionType(Enum::YEAR);
    connect(ui->yearwidget,SIGNAL(CurrentValueChanged(int)),this,SLOT(CurrentValueChanged(int)));
    ui->monthwidget->setRange(Enum::MONTHUP,Enum::MONTHDOWN);
    ui->monthwidget->setSectionType(Enum::MONTH);
    connect(ui->monthwidget,SIGNAL(CurrentValueChanged(int)),this,SLOT(CurrentValueChanged(int)));
    ui->daywidget->setRange(Enum::DAYUP,Enum::DAYUPDOWNOf31);
    ui->daywidget->setSectionType(Enum::DAY);
    connect(ui->daywidget,SIGNAL(CurrentValueChanged(int)),this,SLOT(CurrentValueChanged(int)));
    ui->hourwidget->setRange(Enum::HOURUP,Enum::HOURDOWN);
    ui->hourwidget->setSectionType(Enum::HOUR);
    connect(ui->hourwidget,SIGNAL(CurrentValueChanged(int)),this,SLOT(CurrentValueChanged(int)));
    ui->minutewidget->setRange(Enum::MINUTEUP,Enum::MINUTEDOWN);
    ui->minutewidget->setSectionType(Enum::MINUTE);
    connect(ui->minutewidget,SIGNAL(CurrentValueChanged(int)),this,SLOT(CurrentValueChanged(int)));
    ui->secondwidget->setRange(Enum::SECONDUP,Enum::SECONDDOWN);
    ui->secondwidget->setSectionType(Enum::SECOND);
    connect(ui->secondwidget,SIGNAL(CurrentValueChanged(int)),this,SLOT(CurrentValueChanged(int)));
    ui->pushButton->setStyleSheet("QPushButton{background-color:rgba(0,0,0,0.1);border-radius:5px;text-align:center;font-size:20px;color:rgb(0,191,255);font-weight:800;}"
                                  "QPushButton:hover{background-color:#87CEEB;border-radius:5px;text-align:center;font-size:23px;color:rgb(255,255,255);}");
}

RollingCalendar::~RollingCalendar()
{
    delete ui;
}

void RollingCalendar::CurrentValueChanged(int value)
{
    Q_UNUSED(value);
    currYear = ui->yearwidget->CurrentValue();
    currMonth = ui->monthwidget->CurrentValue();
    ui->daywidget->setRange(1,RollingCalendar::maxDayRange());
}

int RollingCalendar::maxDayRange()
{
    /* 根据年份和月份更新Day的边界值 */
    if(currMonth == 1 || currMonth == 3
        || currMonth == 5 || currMonth == 7
        ||currMonth == 8 || currMonth == 10
        || currMonth == 12)
    {
        updateDayRange = 31;
    }
    else if(currMonth == 4 || currMonth == 6
            || currMonth == 9 || currMonth == 11)
    {
        updateDayRange = 30;
    }
    else if(currMonth == 2 &&
            ((currYear % 4 == 0 && currYear % 100 != 0) || currYear % 400 == 0))
    {
        updateDayRange = 29;
    }
    else {
        updateDayRange = 28;
    }
    return updateDayRange;
}
